/**
 * WeeShop 声明
 * ===========================================================
 * 网站： https://www.darlang.com
 * 标题： ECShop 小程序「weeshop 」- 基于 ECShop 为后台系统开发的非官方微信商城小程序
 * 链接： https://www.darlang.com/?p=709
 * 说明： 源码已开源并遵循 Apache 2.0 协议，你有权利进行任何修改，但请保留出处，请不要删除该注释。
 * ==========================================================
 * Copyright 2019 darlang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===========================================================
 */

// list.js
import { PNT, setNavBarTitle, showToast, pushPagePath, scrollLoadList } from "../../../../utils/utils";
import { DistributDetail } from "../../../../utils/apis";


Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    tabStatus: [{ id: 0, label: '全部' }, { id: 1, label: '未分成' }, { id: 2, label: '已分成' }],
    orderStatus: ['未分成', '已分成'],
    orderLst: [
    ],
    pages: {
      page: 1,
      size: 10,
      total: 10,
      done: false,
      loading: false
    },
    page: 0,
    order_num: ['order', 'pay', 'deliver'],
    type: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    setNavBarTitle(PNT.order.main);
    this.loginModal = this.selectComponent("#login-modal");
    var info = wx.getStorageSync('userInfo')
    this.setData({
      status: opt.status || 0,
      info: info
    });
    this.getOrderList();
    // this.getCancelReason();

  },


  /**
   * 页面跳转
   * @author darlang
   */
  pushPath(e) {
    const items = e.currentTarget.dataset;
    const pathData = [
      { type: 'order', path: '/pages/member/order/detail/detail?id=' + items.id },

      { type: 'comment', path: '/pages/member/comment/add/add?orderId=' + items.id },
      { type: 'payment', path: '/pages/shopping/payment/payment?orderId=' + items.id },

    ];
    pushPagePath(e, pathData);
  },

  /**
   * 登录回调
   * @author darlang
   */
  loginCallback(cb) {
    if (cb.detail.type === 'success') {
      this.getOrderList();
    }
  },
  bindSorderTap(e) {
    this.setData({
      status: e.currentTarget.dataset.id,
      page: 0,
      orderLst: [],
    })
    this.getOrderList()
  },

  /**
   * 获取订单列表
   * @author darlang
   */
  getOrderList() {
    var that = this;
    var data = {};
    data.user_id = this.data.info.user_id;
    data.page = this.data.page;
    data.type = this.data.status;
    DistributDetail(data).then(res => {
      if (res.error_code == 10000) {
        var list = that.data.orderLst;
        for (var i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
        }
        this.setData({
          orderLst: list,
          flag: true,
        })

      }
      wx.hideLoading();
    })
  },
  /**
   * 查找订单位置
   * @author darlang
   * @param  {Number}   id   订单id
   * @param  {String}   type 类型
   */
  findOrderIndex(id, type) {
    let olst = this.data.orderLst;
    let idx = olst.findIndex(k => {
      return k.id == id;
    });
    if (type === 'cancel') {
      type = 5;
    }
    if (type === 'confirm') {
      type === 3;
    }

    olst[idx].status = type;
    this.setData({
      orderLst: olst
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.bindSorderTap();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.flag) {
      wx.showLoading({ title: '加载中...', mask: true });
    }
    var page = this.data.page;
    page = page + 1;
    this.setData({
      page: page,
      flag: false,
    })
    this.getOrderList();
  }
});